package com.crdeng.business.utils;

import com.crdeng.business.vo.ProductCategoryVO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 转换成树结构
 */
public class CategoryTreeBuilder {

    //全部节点
    private List<ProductCategoryVO> all;

    /**
     * 构建菜单树
     * @param productCategoryVOS
     * @return
     */
    public  List<ProductCategoryVO> categoryTreeBuilder(List<ProductCategoryVO> productCategoryVOS){
        all=productCategoryVOS;
        //根节点
        List<ProductCategoryVO> rootNodes = new ArrayList<>();
        for (ProductCategoryVO productCategoryVO : all) {
            //获取根节点
            if (productCategoryVO.getPid()==0){
                //设置层级
                productCategoryVO.setLev(1);
                rootNodes.add(productCategoryVO);
            }
        }

        Collections.sort(rootNodes,ProductCategoryVO.order());
        all.removeAll(rootNodes);

        //获取子节点
        for (ProductCategoryVO rootNode : rootNodes) {
            rootNode.setChildren(getChilds(rootNode));
        }
        return  rootNodes;
    }

    /**
     * 获取子节点
     * @param rootNood
     * @return
     */
    private List<ProductCategoryVO> getChilds(ProductCategoryVO rootNood){
    List<ProductCategoryVO> childs=new ArrayList<>();
        for (ProductCategoryVO productCategoryVO : all) {
            if (rootNood.getId()==productCategoryVO.getPid()){
                productCategoryVO.setLev(rootNood.getLev()+1);
                childs.add(productCategoryVO);
            }
        }
        Collections.sort(childs,ProductCategoryVO.order());
        all.removeAll(childs);

        for (ProductCategoryVO child : childs) {
            if(child.getLev()<3) {
                child.setChildren(getChilds(child));
            }
        }

        return childs;
    }

    /**
     * 构建分层2级树
     * @param productCategoryVOS
     * @return
     */
    public  List<ProductCategoryVO> category2TreeBuilder(List<ProductCategoryVO> productCategoryVOS){
        all=productCategoryVOS;
        //根节点
        List<ProductCategoryVO> rootNodes = new ArrayList<>();
        for (ProductCategoryVO productCategoryVO : all) {
            //获取根节点
            if (productCategoryVO.getPid()==0){
                //设置层级
                productCategoryVO.setLev(1);
                rootNodes.add(productCategoryVO);
            }
        }

        Collections.sort(rootNodes,ProductCategoryVO.order());
        all.removeAll(rootNodes);

        //获取子节点
        for (ProductCategoryVO rootNode : rootNodes) {
            rootNode.setChildren(get2Childs(rootNode));
        }
        return  rootNodes;
    }


    /**
     * 获取子节点到2层
     * @param rootNood
     * @return
     */
    private List<ProductCategoryVO> get2Childs(ProductCategoryVO rootNood){
        List<ProductCategoryVO> childs=new ArrayList<>();
        for (ProductCategoryVO productCategoryVO : all) {
            if (rootNood.getId()==productCategoryVO.getPid()){
                productCategoryVO.setLev(rootNood.getLev()+1);
                childs.add(productCategoryVO);
            }
        }

        return childs;
    }
}
